package com.work.user.controller;

import com.work.entity.Page;
import com.work.entity.PageResult;
import com.work.entity.Result;
import com.work.entity.StatusCode;
import com.work.user.feign.UserFeign;
import com.work.user.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/web/user")
public class UserController {

    @Autowired
    private UserFeign userFeign;

    //分页查询所有用户
    @RequestMapping(value = "/search/{page}/{size}")
    public Result findPage(@RequestParam Map searchMap, @PathVariable("page") int page, @PathVariable("size") int size) {
        try {
            Result pageList = userFeign.findPage(searchMap, page, size);
            //PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
            return pageList;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.FIND_ERROR, "查询失败");
        }
    }

    //多条件分页查询
    @RequestMapping("/search")
    public Result findList(@RequestParam Map searchMap){
        try {
            Result list = userFeign.findList(searchMap);
            return new Result(true, StatusCode.FIND_Ok, "查询成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.FIND_ERROR, "查询失败");
        }
    }

    //添加用户
    @RequestMapping("/add")
    public Result add(@RequestBody User user, @RequestParam("roleIds") Integer[] roleIds) {
        try {
            userFeign.add(user, roleIds);
            return new Result(true, StatusCode.ADD_OK, "添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, StatusCode.ADD_ERROR, "添加失败");
        }
    }

    //修改用户
    @RequestMapping("/update")
    public Result update(@RequestBody User user,  @RequestParam("roleIds")Integer[] roleIds) {
        try {
            userFeign.update(user, roleIds);
            return new Result(true, StatusCode.UPDATE_Ok, "修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, StatusCode.UPDATE_ERROR, "修改失败");
        }
    }

    //删除用户
    @RequestMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        try {
            //超级管理员id=1，不允许删除
            if (id == 1) {
                return new Result(false, StatusCode.DELETE_ERROR, "超级用户不允许删除");
            }
            userFeign.delete(id);
            return new Result(true, StatusCode.DELETE_OK, "删除成功");
        }catch (Exception e){
            return new Result(false,StatusCode.DELETE_ERROR,"删除失败");
        }
    }

    @RequestMapping("/findById")
    public Result findById(@RequestParam Integer id){
        try {
            Result<User> user = userFeign.findById(id);
            return new Result(true, StatusCode.FIND_Ok, "查询成功", user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.FIND_ERROR, "查询失败");
        }
    }

}
